//
//  ChangePwdViewController.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/3/30.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ChangePwdViewController.h"
#import "MineAPIManager.h"
@interface ChangePwdViewController ()<UITextFieldDelegate ,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UIView *tableHeaderView;

@property(nonatomic ,strong)UIView *tableFootorView;

@property(nonatomic ,strong)UIButton *confirmButton;

@property(nonatomic ,strong)UITextField *passwordTextFiledOld;

@property(nonatomic ,strong)UITextField *passwordTextFiledNew;

@property(nonatomic ,strong)UITextField *passwordTextFiledAgain;

@end

@implementation ChangePwdViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self renderNavBarWithBackgroundImage:[UIImage imageWithColor:lWhiteColor] backgroundColor:nil shadowColor:nil textAttributeDic:@{NSForegroundColorAttributeName:rgba(51, 51, 51, 1),NSFontAttributeName:MFontWithSize(18)} enableScrollEdgeAppearance:YES];

}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"修改密码";
    [self.view addSubview:self.tableView];
   
}
#pragma mark -API

- (void)changePwd
{
    NSDictionary *dic = @{@"oldpassword":self.passwordTextFiledOld.text,@"newpassword":self.passwordTextFiledNew.text,@"againpassword":self.passwordTextFiledAgain.text};
    
    [MineAPIManager ChangePwdWithParam:dic result:^(NSDictionary * _Nonnull dic) {
        [lApp goLogin];
    }];
}


#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell =nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    [cell.contentView removeAllSubviews];
  
      if(indexPath.row == 0)
      {
          [cell.contentView addSubview:self.passwordTextFiledOld];
          
      }else if (indexPath.row == 1)
      {
          [cell.contentView addSubview:self.passwordTextFiledNew];
      }else if (indexPath.row == 2)
      {
          [cell.contentView addSubview:self.passwordTextFiledAgain];
          
      }
       
  
    
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        return 3;
        
  
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 62;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


}



#pragma mark -UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    //[[textField viewWithTag:1001] setBackgroundColor:kOrangeLineColor];
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    //[[textField viewWithTag:1001] setBackgroundColor:kLineColor];
    
    return  YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}
#pragma mark - private


#pragma mark -event response
- (void)confirmButtonAction
{
    [self.view endEditing:YES];
    NSString *str = @"";
    if (self.passwordTextFiledOld.text.length == 0) {
        str = @"密码不能为空！";
    }
    
    if (self.passwordTextFiledNew.text.length == 0) {
        str = @"新密码不能为空！";
    }
    
    if (str.length == 0) {
        [self changePwd];
    }else{
        [SVProgressHUD showImage:nil status:str];
    }
}
-(void)textEnd:(UITextField *)textField
{
    
}
-(void)textChanged:(UITextField *)textField
{

}
#pragma mark -getters and setters
-(UITableView *)tableView{
    if (!_tableView){
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth, lScreenHeight) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:lWhiteColor];
        [_tableView setSeparatorColor:rgba(229, 229, 229, 1)];
        _tableView.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
        [_tableView setTableHeaderView:self.tableHeaderView];
        [_tableView setTableFooterView:self.tableFootorView];
    }
    
    return _tableView;
}

-(UIView *)tableHeaderView
{
    if (!_tableHeaderView)
    {
        
        _tableHeaderView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth,76)];
        UIView *viewbg = [[UIView alloc] init];
        viewbg.frame = CGRectMake(0,0,lScreenWidth,36);
        viewbg.backgroundColor = rgba(216, 241, 230, 1);
        [_tableHeaderView addSubview:viewbg];
        
        UIButton *titleLBtn = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake( 15, 0, (lScreenWidth - 60 - 15)/2.0, 36);
            [button setTitle:@"请输入已绑定的手机号" forState:UIControlStateNormal];
            button.titleLabel.font = MFontWithSize(14.f);
            [button setTitleColor:rgba(12, 183, 108, 1) forState:UIControlStateNormal];
            [button setImage:IMAGENAMED(@"ic_warn") forState:UIControlStateNormal];
            button;
        });
        double w = [MethodTool getTextWidthWithStr:titleLBtn.titleLabel.text font:titleLBtn.titleLabel.font ViewHeight:36];
        titleLBtn.width = w + 10 + 16;
        titleLBtn.centerX = viewbg.centerX;
        [titleLBtn layoutButtonWithEdgeInsetsStyle:LButtonEdgeInsetsStyleLeft imageTitleSpace:10];
        [_tableHeaderView addSubview:titleLBtn];
       
       
    }
    
    return _tableHeaderView;
}

-(UIView *)tableFootorView
{
    if (!_tableFootorView)
    {
        _tableFootorView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, lScreenWidth - 20, 20)];
        UIView *loginView =[[UIView alloc] initWithFrame:CGRectMake(20, 65, lScreenWidth - 40, 45)];
        [loginView setAllCornersWithCornerRadius:22.5];
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = CGRectMake(0,0,lScreenWidth - 40,45);
        gl.startPoint = CGPointMake(1, 0.5);
        gl.endPoint = CGPointMake(0, 0.5);
        gl.colors = @[(__bridge id)[UIColor colorWithRed:41/255.0 green:241/255.0 blue:156/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:2/255.0 green:161/255.0 blue:249/255.0 alpha:1.0].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        [loginView.layer addSublayer:gl];
        [loginView addSubview:self.confirmButton];
       
        [_tableFootorView addSubview:loginView];
        _tableFootorView.height =loginView.bottom + 15;
    }
    
    return _tableFootorView;
}


-(UIButton *)confirmButton
{
    if (!_confirmButton)
    {
        _confirmButton = [UIButton new];
        _confirmButton.frame =CGRectMake(0, 0, lScreenWidth - 40, 45);
       
        [_confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        //[_confirmButton setTitleColor:rgba(181, 181, 181, 1) forState:UIControlStateNormal];
        [_confirmButton setTitleColor:lWhiteColor forState:UIControlStateNormal];
        [_confirmButton.titleLabel setFont:MFontWithSize(18)];
        [_confirmButton addTarget:self action:@selector(confirmButtonAction) forControlEvents:UIControlEventTouchUpInside];
        //_confirmButton.backgroundColor = rgba(229, 229, 229, 1);
        
    }
    
    return _confirmButton;
}

-(UITextField *)passwordTextFiledOld
{
    if (!_passwordTextFiledOld)
    {
        _passwordTextFiledOld =[[UITextField alloc] initWithFrame:CGRectMake(20, 0, lScreenWidth - 40, 62)];
        _passwordTextFiledOld.font = MFontWithSize(16);
        _passwordTextFiledOld.textColor = rgba(51, 51, 51, 1);
        _passwordTextFiledOld.clearButtonMode =UITextFieldViewModeWhileEditing;
        _passwordTextFiledOld.secureTextEntry =YES;
        _passwordTextFiledOld.delegate =self;
        _passwordTextFiledOld.placeholder = @"请输入密码";
        

        if (@available(iOS 11.0,*)) {
            _passwordTextFiledOld.textContentType =UITextContentTypePassword;
        }
    
    }

    return _passwordTextFiledOld;
}
-(UITextField *)passwordTextFiledNew
{
    if (!_passwordTextFiledNew)
    {
        _passwordTextFiledNew =[[UITextField alloc] initWithFrame:CGRectMake(20, 0, lScreenWidth - 40, 62)];
        _passwordTextFiledNew.font = MFontWithSize(16);
        _passwordTextFiledNew.textColor = rgba(51, 51, 51, 1);
        _passwordTextFiledNew.clearButtonMode =UITextFieldViewModeWhileEditing;
        _passwordTextFiledNew.secureTextEntry =YES;
        _passwordTextFiledNew.delegate =self;
        _passwordTextFiledNew.placeholder = @"请输入新密码";
        

        if (@available(iOS 11.0,*)) {
            _passwordTextFiledNew.textContentType =UITextContentTypePassword;
        }
    
    }

    return _passwordTextFiledNew;
}
-(UITextField *)passwordTextFiledAgain
{
    if (!_passwordTextFiledAgain)
    {
        _passwordTextFiledAgain =[[UITextField alloc] initWithFrame:CGRectMake(20, 0, lScreenWidth - 40, 62)];
        _passwordTextFiledAgain.font = MFontWithSize(16);
        _passwordTextFiledAgain.textColor = rgba(51, 51, 51, 1);
        _passwordTextFiledAgain.clearButtonMode =UITextFieldViewModeWhileEditing;
        _passwordTextFiledAgain.secureTextEntry =YES;
        _passwordTextFiledAgain.delegate =self;
        _passwordTextFiledAgain.placeholder = @"请确认新密码";
        

        if (@available(iOS 11.0,*)) {
            _passwordTextFiledAgain.textContentType =UITextContentTypePassword;
        }
    
    }

    return _passwordTextFiledAgain;
}
@end
